//
//  GNStatusToolBar.swift
//  SinaWeChat
//
//  Created by mac on 15/12/10.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class GNStatusToolBar: UIView {
    
    var retweetButton: UIButton?
    var commentButton: UIButton?
    var unlikeButton: UIButton?
    
    var statusViewModel:GNStatusViewModel?{
        
        didSet{
            
            //设置数据
            
            //取到三个按钮,设置按钮上的文字
            retweetButton?.setTitle(statusViewModel?.retweetCountStr, forState: .Normal)
            
            commentButton?.setTitle(statusViewModel?.commentCountStr, forState: .Normal)
            unlikeButton?.setTitle(statusViewModel?.attitudeCountStr, forState: .Normal)
            
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置视图
    
    private func setupUI(){
        
        //添加三个按钮
        
        retweetButton = addchildButton("转发", imageName: "timeline_icon_retweet")
        commentButton = addchildButton("评论", imageName: "timeline_icon_comment")
        unlikeButton = addchildButton("赞", imageName: "timeline_icon_unlike")
        
        //添加俩个分割线
        
        let sp1 = addSeparator()
        let sp2 = addSeparator()
        
        //添加约束
        retweetButton?.snp_makeConstraints(closure: { (make) -> Void in
            
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        })
        
        commentButton?.snp_makeConstraints(closure: { (make) -> Void in
            
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(unlikeButton!)
            
        })
        
        unlikeButton?.snp_makeConstraints(closure: { (make) -> Void in
            
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        })
        
        
        //添加分割线的约束
        sp1.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(retweetButton!.snp_trailing)
            make.centerY.equalTo(self)
            
        }
        
        sp2.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(commentButton!.snp_trailing)
            make.centerY.equalTo(self)
        }
    }
    
    //添加三个按钮
    
    private func addchildButton(title:String,imageName:String) -> UIButton {
        
        let button = UIButton()
        
        //设置文字,字体大小和颜色
        button.setTitle(title, forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Highlighted)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        //设置图标
        button.setImage(UIImage(named: imageName), forState: .Normal)
        
        //设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: .Highlighted)
        
        addSubview(button)
        
        return button
        
    }
    
    //添加分割线
    private func addSeparator() -> UIView {
        
        let iamgeView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        
        addSubview(iamgeView)
        
        return iamgeView
    }
    
}
